package com.itcast.main.service.transit.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itcast.main.dao.take_delivery.WayBillRepository;
import com.itcast.main.dao.transit.TransitInfoRepository;
import com.itcast.main.domain.take_delivery.WayBill;
import com.itcast.main.domain.transit.TransitInfo;
import com.itcast.main.service.transit.TransitInfoService;
@Service
@Transactional
public class TransitInfoServiceImpl implements TransitInfoService {
	
	@Autowired
	private WayBillRepository wayBillRepository;
	
	
	@Autowired
	private TransitInfoRepository transitInfoRepository;

	@Override
	public void createTransits(String wayBillIds) {
		if (StringUtils.isNoneBlank(wayBillIds)) {
			//处理运单
			for (String WayBillId : wayBillIds.split(",")) {
				WayBill wayBill = wayBillRepository.findOne(Integer.parseInt(WayBillId));
				//判断运单状态是否为待发货
				if (wayBill.getSignStatus() == 1) {
					//代发货
					//生成TransitInfo信息
					TransitInfo transitInfo = new TransitInfo();
					transitInfo.setWayBill(wayBill);
					transitInfo.setStatus("出入库中转");
					transitInfoRepository.save(transitInfo);
					
					//更改运单状态
					wayBill.setSignStatus(2);
				}
			}
		}
		
	}

	@Override
	public Page<TransitInfo> findPageDate(Pageable pageable) {
		// TODO Auto-generated method stub
		return transitInfoRepository.findAll(pageable);
	}
}
